<?php

namespace DatMoment\VideoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Video
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="DatMoment\VideoBundle\Entity\VideoRepository")
 */
class Video
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="idVideo", type="string")
     */
    private $idVideo;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", length=255)
     */
    private $type;

    /**
     * @var string
     *
     * @ORM\Column(name="titre", type="string", length=255)
     */
    private $titre;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255)
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="code", type="string", length=255)
     */
    private $code;

    /**
     * @var string
     *
     * @ORM\Column(name="img", type="string", length=255)
     */
    private $img;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dateAdd", type="datetime")
     */
    private $dateAdd;
    
    /**
     * @var string
     * 
     * @ORM\Column(name="url", type="string", length=255)
     */
    private $url;


    /**
     * @ORM\ManyToOne(targetEntity="DatMoment\VideoBundle\Entity\Jeux", cascade={"persist"})
     * 
     */
    private $jeux;
    
     /**
     * @ORM\ManyToOne(targetEntity="DatMoment\UserBundle\Entity\User", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;
    
    /**
     * @ORM\ManyToOne(targetEntity="DatMoment\VideoBundle\Entity\Categorie", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $categorie;
    
    
    /**
     * @ORM\OneToMany(targetEntity="DatMoment\VideoBundle\Entity\Appreciation", mappedBy="video", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $appreciations;
    
    
    public function __construct() {
        $this->appreciations = new \Doctrine\Common\Collections\ArrayCollection();
        $this->setDateAdd();
    }
    
    public function setUser($puser) {
        $this->user = $puser;
    }
    
    public function getUser() {
        return $this->user;
    }
    
    public function setJeux($pjeux) {
       $this->jeux = $pjeux;
    }
    
    public function setUrl($purl) {
        $this->url = $purl;
    }
    
    public function getUrl() {
        return $this->url;
    }
    
     /**
     * Get id
     *
     * @return integer 
     */
    public function getJeux() {
        return $this->jeux;
    }
    
    public function setCategorie($pcategorie) {
        $this->categorie = $pcategorie;
    }
    
     public function addAppreciations($pappreciation) {
        $this->appreciations[] = $pappreciation;
    }
    
    
    public function getAppreciations() {
        return $this->appreciations;
    }
    
    public function getCategorie() {
        return $this->categorie;
    }
    
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set idVideo
     *
     * @param integer $idVideo
     * @return Video
     */
    public function setIdVideo($idVideo)
    {
        $this->idVideo = $idVideo;

        return $this;
    }

    /**
     * Get idVideo
     *
     * @return integer 
     */
    public function getIdVideo()
    {
        return $this->idVideo;
    }

    /**
     * Set type
     *
     * @param string $type
     * @return Video
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return string 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set titre
     *
     * @param string $titre
     * @return Video
     */
    public function setTitre($titre)
    {
        $this->titre = $titre;

        return $this;
    }

    /**
     * Get titre
     *
     * @return string 
     */
    public function getTitre()
    {
        return $this->titre;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Video
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set code
     *
     * @param string $code
     * @return Video
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * Get code
     *
     * @return string 
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * Set img
     *
     * @param string $img
     * @return Video
     */
    public function setImg($img)
    {
        $this->img = $img;

        return $this;
    }

    /**
     * Get img
     *
     * @return string 
     */
    public function getImg()
    {
        return $this->img;
    }

    /**
     * Set dateAdd
     *
     * @param \DateTime $dateAdd
     * @return Video
     */
    public function setDateAdd()
    {
        $this->dateAdd = new \DateTime("now");

        return $this;
    }

    /**
     * Get dateAdd
     *
     * @return \DateTime 
     */
    public function getDateAdd()
    {
        return $this->dateAdd;
    }
    
    public function getCategoriesString() {
        $listCategories = $this->getCategories();
        $categoriesString = "";
        $first = true;
        foreach ($listCategories as $categorie) {
            if ($first) {
                $categoriesString .= $categorie->getName();
                $first = false;
            }
            else {
                $categoriesString .= " , " .$categorie->getName(); 
            }
        }
        return $categoriesString;
    }
    
    /**
     * méthode qui calcul la note sur l'ensemble des commentaires.
     * @return float au dixième supérieur pret
     */
    public function getNoteVideo() {
        $resultat = 0;
        foreach ($this->getAppreciations() as $appre) {
            $resultat += $appre->getNote();
        }
        if (count($this->getAppreciations()) != 0) {
            $resultat = $resultat / count($this->getAppreciations());
        }
        return round($resultat,1,PHP_ROUND_HALF_UP);
    }
    
    /**
     * méthode qui renvoie le nombre de commentaires.
     * @return integer
     */
    public function getNbCommentaireVideo() {
        return count($this->getAppreciations());
    }
}
